package com.zhimeng.personal.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by sunzhimeng on 11/6/17.
 */

public class ViewUtils {

    public static void resetSqareView(View view) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        lp.height = view.getMeasuredWidth();
        view.setLayoutParams(lp);
    }

    public static void setViewDemesion(View view, int width, int height) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        lp.width = width;
        lp.height = height;
        view.setLayoutParams(lp);
    }

    public static int[] getViewPosition(View view) {
        int[] point = new int[2];
        view.getLocationInWindow(point);
        return point;
    }

    public static void getViewSpec(final View view, final ViewSpecCallback callback) {
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @SuppressLint("NewApi")
            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
                //now we can retrieve the width and height
                int width = view.getWidth();
                int height = view.getHeight();
                if (callback != null) {
                    callback.onMeasured(width, height);
                }
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                    view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            }
        });
    }

    public static void resetSizeForView(Context mContext, View view, int dpWidth, int dpHeight) {
        int width = DensityUtils.dp2px(mContext, dpWidth);
        int height = DensityUtils.dp2px(mContext, dpHeight);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height);
        view.setLayoutParams(lp);
    }

    public static void boldTextView(TextView tv) {
        TextPaint tp = tv.getPaint();
        tp.setFakeBoldText(true);
    }

    public static void resetWidth(View view, int width) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        lp.width = width;
        view.setLayoutParams(lp);
    }

    public static void resetHeight(View view, int height) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        lp.height = height;
        view.setLayoutParams(lp);
    }

    private static void setRecycleviewOrientation(Context context, RecyclerView recyclerView, int orientation){
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(orientation);
        recyclerView.setLayoutManager(layoutManager);
    }

    public static void setRecycleviewVerticle(Context context, RecyclerView recyclerView){
        setRecycleviewOrientation(context, recyclerView, LinearLayoutManager.VERTICAL);
    }

    public static void setRecycleviewHorizontal(Context context, RecyclerView recyclerView){
        setRecycleviewOrientation(context, recyclerView, LinearLayoutManager.HORIZONTAL);
    }

    public interface ViewSpecCallback {
        void onMeasured(int width, int height);
    }
}
